import {Component} from "@angular/core";
import {Events, IonicPage, NavController, NavParams, ToastController} from "ionic-angular";
import {AddStaffTrack, TrackStatus} from "../../../model/pages/activity/roster-list/roster-list-item.model";
import {
    SingleRosterFilterModel
} from "../../../model/pages/activity/roster-filter/roster-filter.model";
import {DateService} from "../../../services/common/date.service";

@IonicPage()
@Component({
    selector: 'page-activity-roster-filter',
    templateUrl: 'activity-roster-filter.html',
    providers: [DateService]
})
export class ActivityRosterFilterPage {

    //筛选条件对象
    private rosterFilteModel: SingleRosterFilterModel = new SingleRosterFilterModel();

    private followList = [{
        id: TrackStatus.notFollow,
        name: '未跟进'
    },{
        id: TrackStatus.contacted,
        name: '已拨打'
    },{
        id: TrackStatus.notContacted,
        name: '未接通'
    },{
        id: TrackStatus.notInterested,
        name: '客户不感兴趣'
    },{
        id: TrackStatus.waitCallback,
        name: '待回电'
    },{
        id: TrackStatus.overInterview,
        name: '已面谈'
    },{
        id: TrackStatus.overPrint,
        name: '已打印计划书'
    },{
        id: TrackStatus.complete,
        name: '已完成'
    }];

    private followList2 = [{
        id: AddStaffTrack.notFollow,
        name: '未跟进'
    },{
        id: AddStaffTrack.process,
        name: '跟进中'
    },{
        id: AddStaffTrack.notIntend,
        name: '暂不跟进'
    }];
    //时间错误
    private hasTimeError = false;
    private yearList:any[]=[];

    private pickerOptions = {
        cssClass: 'roster-filter_picker'
    };

    constructor(private events: Events,
                private navCtrl: NavController,
                private navParams: NavParams,
                public dateService:DateService,
                private toastCtrl: ToastController) {
        this.yearList=this.dateService.getYearList(1996);
    }

    ionViewDidEnter() {
        this.rosterFilteModel = this.navParams.get('tempFil');
    }

    changeFilter(key, val) {
        let index = this.rosterFilteModel[key].indexOf(val);
        if (index === -1) {
            this.rosterFilteModel[key].push(val);
        } else {
            this.rosterFilteModel[key].splice(index, 1);
        }
    }

    isSelectBtn(key, val) {
        return !(this.rosterFilteModel[key].indexOf(val) === -1);
    }

    resetForm() {
        this.rosterFilteModel.trackStatus = [];
        this.rosterFilteModel.startTakeTime = '';
        this.rosterFilteModel.endTakeTime = '';
    }

    doSearch() {
        this.events.publish('activity-roster:search', this.rosterFilteModel);
        this.navCtrl.pop();
    }

    validateTime () {
        if (this.rosterFilteModel.startTakeTime && this.rosterFilteModel.endTakeTime) {
            if (new Date(this.rosterFilteModel.startTakeTime) > new Date(this.rosterFilteModel.endTakeTime)) {
                this.hasTimeError = true;
                this.toastCtrl.create({
                    message: '活动参与时间输入有误',
                    duration: 1500,
                    position: 'middle'
                }).present();
            } else {
                this.hasTimeError = false;
            }
        } else {
            this.hasTimeError = false;
        }
    }

    cleanTakeTime () {
        this.rosterFilteModel.startTakeTime = this.rosterFilteModel.endTakeTime = null;
    }
}
